#include "sdk.h"
#include "TextFileSearcherRegEx.h"

TextFileSearcherRegEx::TextFileSearcherRegEx( const wxString& searchText, bool matchCase, bool matchWordBegin,
    bool matchWord )
  : TextFileSearcher( searchText, matchCase, matchWordBegin, matchWord ) {
  wxString pattern = searchText;
  int flags = wxRE_ADVANCED;
  if( matchCase == false ) {
    flags |= wxRE_ICASE;
  }
  if( matchWord == true ) {
    pattern = _T( "([^[:alnum:]_]|^)" ) + pattern + _T( "([^[:alnum:]_]|$)" );
  } else if( matchWordBegin == true ) {
    pattern = _T( "([^[:alnum:]_]|^)" ) + pattern;
  }
  m_RegEx.Compile( pattern, flags );
}


bool TextFileSearcherRegEx::MatchLine( wxString line ) {
  bool match = false;
  if( m_RegEx.IsValid() ) {
    match = m_RegEx.Matches( line.c_str() );
  }
  return match;
}


bool TextFileSearcherRegEx::IsOk( wxString* pErrorMessage ) {
  bool ok = m_RegEx.IsValid();
  if( !ok && pErrorMessage ) {
    *pErrorMessage = _T( "无效表达式" );
  }
  return ok;
}
